/**
 * @author Tobias Boehm
 *
 * 25.03.2011
 */
package org.anddev.andengine.extension.tbsengine.map;

import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXObjectGroup;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMapProperty;
import org.anddev.andengine.extension.tbsengine.gameobject.GameObject;
import org.anddev.andengine.extension.tbsengine.gameobject.Unit;
import org.anddev.andengine.util.path.ITiledMap;

/**
 * This is an abstract representation of a map. It contains a {@link TMXTiledMap}
 * and creates shortcuts to specific tile layers, object groups and parameters
 * described in {@link TBSmapConstants} to provide easy access.
 * <p>
 * It also enforces the implementation of {@link ITiledMap} to provide an 
 * interface for path finding algorithms.
 * <p>
 * Warning:<br>
 * This class is in the progress of development. It may change dramatically 
 * especially in the context of where and how to store {@link GameObject}s  
 * related to the {@link TMXObjectGroup}s.  
 * 
 * 
 * @author Tobias Boehm
 *
 */
public abstract class AbsMap implements ITiledMap<Unit>, TBSmapConstants{

	/**
	 * A reference to the underlying {@link TMXTiledMap}.
	 */
	protected final TMXTiledMap mTMXtiledMap;	
	
	/**
	 * A shortcut to the {@link BaseMapTileLayer#ground} layer
	 * inside the {@link TMXTiledMap}.
	 */
	protected TMXLayer mGroundLayer;
	
	/**
	 * A shortcut to the {@link BaseMapTileLayer#decorations} layer
	 * inside the {@link TMXTiledMap}.
	 */
	protected TMXLayer mDecorationLayer;
	
	/**
	 * A shortcut to the {@link BaseMapObjectGroup#doodads} layer
	 * inside the {@link TMXTiledMap}.
	 */
	protected TMXObjectGroup mDoodadObjectGroup;
	
	/**
	 * A shortcut to the {@link BaseMapObjectGroup#playergameobjects} layer
	 * inside the {@link TMXTiledMap}.
	 */
	protected TMXObjectGroup mGameObjectGroup;
	
	/**
	 * A shortcut to the {@link BaseMapObjectGroup#areas} layer
	 * inside the {@link TMXTiledMap}.
	 */
	protected TMXObjectGroup mAreaObjectGroup;
	
	/**
	 * The map ID.
	 */
	protected String mMapID;
	
	/**
	 * The map name.
	 */
	protected String mMapName;
	
	/**
	 * The map version number.
	 */
	protected int mMapVersion;
	
	
	/**
	 * This is the default constructor which creates a map including some 
	 * shortcuts to important components based on the provided
	 * {@link TMXTiledMap}.
	 * 
	 * @param pTMXtiledMap the {@link TMXTiledMap} this map will be based on.
	 * 
	 * @see #createShortcuts()
	 */
	public AbsMap(final TMXTiledMap pTMXtiledMap) {
	
		this.mTMXtiledMap = pTMXtiledMap;
	
		this.createShortcuts();
	}
	
	/**
	 * This method creates the shortcuts mentioned in the constructor's 
	 * description.
	 * 
	 * @see #AbsMap(TMXTiledMap)
	 */
	protected void createShortcuts() {
		
		// Create TileLayer Shortcuts
		for(TMXLayer currentLayer : this.mTMXtiledMap.getTMXLayers()){
			
			if(currentLayer.getName().equals(BaseMapTileLayer.ground.name())){
			
				this.mGroundLayer = currentLayer;
				
			}else if(currentLayer.getName().equals(BaseMapTileLayer.decorations.name())){
				
				this.mDecorationLayer = currentLayer;
			}
		}

		// Create ObjectGroup Shortcuts
		for(TMXObjectGroup currentGroup : this.mTMXtiledMap.getTMXObjectGroups()){
			
			if(currentGroup.getName().equals(BaseMapObjectGroup.doodads.name())){
			
				this.mDoodadObjectGroup = currentGroup;
				
			}else if(currentGroup.getName().equals(BaseMapObjectGroup.playergameobjects.name())){
			
				this.mGameObjectGroup = currentGroup;
				
			}else if(currentGroup.getName().equals(BaseMapObjectGroup.areas.name())){
			
				this.mAreaObjectGroup = currentGroup;
			}
		}
		
		// Create some map property shortcuts
		for(TMXTiledMapProperty mapProperty : this.mTMXtiledMap.getTMXTiledMapProperties()){
			
			if(mapProperty.getName().equals(BaseMapProperty.id.name())){
				
				this.mMapID = mapProperty.getValue();
				
			}else if(mapProperty.getName().equals(BaseMapProperty.name.name())){
				
				this.mMapName = mapProperty.getValue();
				
			}else if(mapProperty.getName().equals(BaseMapProperty.version.name())){
				
				this.mMapVersion = new Integer(mapProperty.getValue());
			}
		}
				
		this.createAdditionalShortcuts(this.mTMXtiledMap);
	}

	/**
	 * In this method you may create additional shortcuts which are not part of 
	 * components described in {@link TBSmapConstants}.
	 * 
	 * @param pTMXtiledMap the {@link TMXTiledMap} this map will be based on.
	 */
	protected abstract void createAdditionalShortcuts(final TMXTiledMap pTMXtiledMap);


	/**
	 * @return the mGroundLayer
	 */
	public TMXLayer getGroundLayer() {
		return mGroundLayer;
	}


	/**
	 * @return the mDecorationLayer
	 */
	public TMXLayer getDecorationLayer() {
		return mDecorationLayer;
	}


	/**
	 * @return the mGameObjectGroup
	 */
	public TMXObjectGroup getGameObjectGroup() {
		return mGameObjectGroup;
	}


	/**
	 * @param mGameObjectGroup the mGameObjectGroup to set
	 */
	public void setGameObjectGroup(TMXObjectGroup mGameObjectGroup) {
		this.mGameObjectGroup = mGameObjectGroup;
	}


	/**
	 * @return the mMapID
	 */
	public String getMapID() {
		return mMapID;
	}


	/**
	 * @return the mMapVersion
	 */
	public int getMapVersion() {
		return mMapVersion;
	}


	/**
	 * @return the mMapName
	 */
	public String getMapName() {
		return mMapName;
	}
	

}
